package cn.mesmile.thread.aqs;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * @author zb
 * @date 2021/5/30 19:17
 * @Description:
 */
public class UnsafeInstance {

    private static Unsafe unsafe;

    private UnsafeInstance(){}

    public static Unsafe reflectGetUnsafe() {
        if (unsafe == null) {
            synchronized (UnsafeInstance.class) {
                if (unsafe == null) {
                    try {
                        Field field = Unsafe.class.getDeclaredField("theUnsafe");
                        field.setAccessible(true);
                        unsafe = (Unsafe) field.get(null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return unsafe;
    }



}
